/****************************************************************
   infile simulated individual data
   generate profiles
*****************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;


****************************************************************;
**   Simulated individuals;
****************************************************************;

*define programs;
*plot profiles for each type of het;
cap program drop label_profiles; 
program define label_profiles;
   label var ihet "Heterogeneity";
   label var t "Age";
   label var A "Asset";
   label var H "Human Capital: all";
   label var AIME "AIME";
   label var C "Consumption";
   label var C_orig "Consumption";
   label var I "Investment";
   label var I_cond "Investment at work";
   label var L "Leisure";
   label var labor "Actual labor";
   label var olabor "Observed labor";
   label var EV "Value incl leisure";
   label var V0 "Value (L=0)";
   label var V1 "Value (L=1)";
   label var probV0 "Prob of L=0";
   label var Vp "Value (L=p)";
   label var probVp "Prob of L=p";
   label var inc "Income";
   label var lnw "Mean log measured wages";
   *label var lnw_fe "Mean log measured wages (FE)";
   label var lnw_fd "Mean log measured wages (FD)";

   label var lnH "log(H): all";
   label var H_worker "H: working";
   label var lnH_worker "log(H): working";

   label var s_irecss "SS status";
   label var irecss "Receive SS";
   label var ssa "Social Security Application";
   *label var goodhealth "Good health";
   label var lfpr "Employment rate (ER)";  // conditional on not working PT
   label var lfpr_ft "Employment rate (ER), Full Time";  // unconditional
   label var lfpr_pt "Employment rate (ER), Part Time";  // unconditional
   label var lfpr1 "ER: Excellent";
   label var lfpr2 "ER: Good";
   label var lfpr3 "ER: Bad";
   label var lfpr4 "ER: Disabled";
   label var lnw1 "lnw: Excellent";
   label var lnw2 "lnw: Good";
   label var lnw3 "lnw: Bad";
   label var lnw4 "lnw: Disabled";
   label var lfpr_diff_E2G "diff in ER: E - G";
   label var lfpr_diff_G2B "diff in ER: G - B";
   label var lfpr_diff_B2D "diff in ER: B - D";
   label var lnw_diff_E2G "diff in lnw: E - G";
   label var lnw_diff_G2B "diff in lnw: G - B";
   label var lnw_diff_B2D "diff in lnw: B - D";
   label var sdlnw "S.D. log measured wages";
   label var sdH "S.D. Human capital";
   label var sdA "S.D. Asset";
   label var sdAIME "S.D. AIME";
   label var FS_spinc "Spouse income";
   label var iFS "Family status";
   label var lfpr1to0 "E to U";
   label var lfpr0to1 "U to E";
   label var alfpr1to0 "Aggregate E to U";
   label var alfpr0to1 "Aggregate U to E";
   label var pH18 "H depreciation after unemployment";
   label var pH41 "H depreciation after unemployment";
   label var pH218 "H depreciation after unemployment";
   label var pH241 "H depreciation after unemployment";
   label var plnw18 "lnw depreciation after unemployment";
   label var plnw41 "lnw depreciation after unemployment";
   label var p1A "p1: Assets";
   label var p2A "p2: Assets";
   label var p5A "p5: Assets";
   label var p50A "p50: Assets";
   label var p95A "p95: Assets";
   label var p98A "p98: Assets";
   label var p99A "p99: Assets";
   label var p1H "p1: Human Capital";
   label var p2H "p2: Human Capital";
   label var p5H "p5: Human Capital";
   label var p50H "p50: Human Capital";
   label var p95H "p95: Human Capital";
   label var p98H "p98: Human Capital";
   label var p99H "p99: Human Capital";
   cap label var lnw_corr "lnw correlation";
   label var nn "#obs";
end;

/*********************************************************************
    Simulated individual data;
    Generate profiles;
*********************************************************************/
prog_MSM_indiv_infile "${cwd_indiv}/txt_MSM_Indiv.txt";

save ${gdtadir}/dta_MSM_Indiv.dta, replace;

gen single = (iFS == 1);
gen mrdspnw = (iFS == 2);
gen mrdspw = (iFS == 3);
label var single "Single";
label var mrdspnw "Married, spouse not working";
label var mrdspw "Married, spouse working";

compress;

* by family status: iFS 1- single, 2- spouse not working, 3- spouse working;

local lvprofiles A H AIME C I I_cond L labor lfpr lfpr_ft lfpr_pt olabor inc lnw  // lnw_fe_full lnw_fe 
                 dlnw dlnw_full dlnw_base dlnw_full_base lnH H_worker lnH_worker 
                 s_irecss irecss ssa EV V0 V1 probV0 Vp probVp FS_spinc C_orig single mrdspnw mrdspw;

* generate and plot profiles from individual for all or for each het group;
foreach ivhet in 0 1 2 {;
   if ("`ivhet'"=="0") {;
      local lvhet "";
      local lvhetcmd "gen byte ihet = 0";
   };
   else if ("`ivhet'"=="1") {;
      local lvhet ihet;
      local lvhetcmd disp "";
   };
   else if ("`ivhet'"=="2") {;
      local lvhet iFS;
      local lvhetcmd "gen int ihet = 100 + iFS";
   };

   compress;

   * profiles;
   preserve;
      collapse (mean) `lvprofiles' (count) nn=id
               (sd) sdlnw=lnw sdH=H sdA=A sdAIME=AIME sdC=C sdinc=inc sdlfpr=lfpr 
               (p1) p1A=A p1H=H (p2) p2A=A p2H=H (p5) p5A=A p5H=H 
               (p50) p50A=A p50H=H p50lnw=lnw p50inc=inc p50C=C p50I=I p50I_cond=I_cond p50labor=labor  //p50lnw_fe=lnw_fe
               (p95) p95A=A p95H=H (p98) p98A=A p98H=H (p99) p99A=A p99H=H, by(t `lvhet');
      `lvhetcmd';
      sort ihet t;
      by ihet: gen lfpr_exit = lfpr[_n-1] - lfpr;

      replace dlnw = dlnw_base if t==${gvdata0}; 
      by ihet: gen lnw_fd = sum(dlnw) if t>=${gvdata0};
      
      replace dlnw_full = dlnw_full_base if t==${gvdata0}; 
      by ihet: gen lnw_fd_full = sum(dlnw_full) if t>=${gvdata0};  // here sum() does not include anything before ${gvdata0}
      
      if ("`ivhet'" != "0") {;
         append using ${gdtadir}/dta_MSM_Profiles.dta;
      };

      sort ihet t;
      save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;
   * lfpr_diff and lnw_diff;
   preserve;
      collapse (mean) lfpr lnw, by(`lvhet' t ihealth);
      reshape wide lfpr lnw, i(`lvhet' t) j(ihealth);
      `lvhetcmd';
      foreach iiv in lfpr lnw {;
         cap gen `iiv'2 = .;
         cap gen `iiv'3 = .;
         cap gen `iiv'4 = .;
         gen `iiv'_diff_E2G = `iiv'1 - `iiv'2;
         gen `iiv'_diff_G2B = `iiv'2 - `iiv'3;
         gen `iiv'_diff_B2D = `iiv'3 - `iiv'4;
      };
      keep ihet t lfpr_diff_* lnw_diff_* lfpr? lnw?;
      sort ihet t;
      merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
      sort ihet t;
      save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;

   * lfpr1to0, lfpr0to1, excluding PT;
   preserve;
      keep if t>=obs_start & t+1<=obs_end;
      drop if lfpr >= .;
      collapse (mean) lfpr_trans = lfpr_next, by(`lvhet' t lfpr);
      reshape wide lfpr_trans, i(`lvhet' t) j(lfpr);
      `lvhetcmd';
      ren lfpr_trans1 lfpr1to0;
      replace lfpr1to0 = 1 - lfpr1to0;
      ren lfpr_trans0 lfpr0to1;
      keep ihet t lfpr1to0 lfpr0to1;
      sort ihet t;
      merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
      sort ihet t;
      save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;
   * aggregate lfpr1to0, lfpr0to1;
   preserve;
      keep if t>=obs_start & t+1<=obs_end;
      drop if lfpr >= .;
      keep if t >= 35 & t <= 50;
      collapse (mean) alfpr_trans = lfpr_next, by(`lvhet' lfpr);
      `lvhetcmd';
      reshape wide alfpr_trans, i(ihet) j(lfpr);
      ren alfpr_trans1 alfpr1to0;
      replace alfpr1to0 = 1 - alfpr1to0;
      ren alfpr_trans0 alfpr0to1;
      keep ihet alfpr1to0 alfpr0to1;
      sort ihet;
      merge 1:m ihet using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
      sort ihet t;
      save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;
   * consumption conditional on lfpr;
   preserve;
      drop if lfpr >= .;
      collapse (mean) C_lfpr=C, by(`lvhet' t lfpr);
      reshape wide C_lfpr, i(`lvhet' t) j(lfpr);
      `lvhetcmd';
      sort ihet t;
      merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
      sort ihet t;
      save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;

   * changes in the human capital and wage level after one spell of unemployment;
   * working at t-1 and t+1, not working at t;
   *if (`ivhet'==0) {;
      preserve;
         sort id t;
         by id: gen qual = (lfpr_ft[_n-2]==1 & t[_n-2]==t-2 & lfpr[_n-1]==0 & t[_n-1]==t-1 & lfpr_ft==1);  
         // t-2: FT, t-1: not working, t: Full time, unemp, no PT;
         foreach iv in H lnw {;
            by id: gen `iv'_0 = `iv'[_n-2];
            by id: gen `iv'_1 = `iv';
         };
         by id: gen H_p = H[_n-1];
         keep if t>=obs_start & t<=obs_end;  // in Code we use t as the reference point and compare with t-2
         keep if qual==1;
         gen pH = ln(H_1) - ln(H_p);
         gen pH2 = ln(H_1) - ln(H_0);
         gen plnw = lnw_1 - lnw_0;

         foreach iv in pH pH2 plnw {;
             gen `iv'18 = `iv' if t>=18+4*${gIcol} & t<=65;
             gen `iv'41 = `iv' if t>40 & t<=65;
         };
         if (`ivhet'==0) {;
             collapse (mean) pH18 pH218 plnw18 pH41 pH241 plnw41
                      (median) pH18_median=pH18 pH218_median=pH218 plnw18_median=plnw18 
                               pH41_median=pH41 pH241_median=pH241 plnw41_median=plnw41;
         };
         else {;
             collapse (mean) pH18 pH218 plnw18 pH41 pH241 plnw41
                      (median) pH18_median=pH18 pH218_median=pH218 plnw18_median=plnw18 
                               pH41_median=pH41 pH241_median=pH241 plnw41_median=plnw41, by(`lvhet');
         };
         `lvhetcmd';

      *   sum pH plnw;
      *   foreach iv in pH pH2 plnw {;
      *      sum `iv' if t>=18+4*${gIcol} & t<=65, detail;
      *      gen `iv'18_mean = round(r(mean),.001);
      *      gen `iv'18_median = round(r(p50),.001);
      *      sum `iv' if t>40 & t<=65, detail;
      *      gen `iv'41_mean = round(r(mean),.001);
      *      gen `iv'41_median = round(r(p50),.001);
      *   };
      *   keep if [_n] == 1;
      *   keep p*_mean p*_median;
      *   foreach iv in pH pH2 plnw {;
      *       ren `iv'18_mean `iv'18;
      *       ren `iv'41_mean `iv'41;
      *   };
      *   gen ihet = 0;
         sort ihet;
         merge 1:m ihet using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
         sort ihet t;
         save ${gdtadir}/dta_MSM_Profiles.dta, replace;
      restore;
   *};

   if (1>10 & "`ivhet'"=="0") {;  // only for overall, not each ihet type;
       *correlation;
       foreach jjv in H A {;
           preserve;
               sort `lvhet' t;
               statsby corr_lfpr_`jjv'=r(rho), by(`lvhet' t) clear: corr lfpr `jjv';
               `lvhetcmd';
               sort ihet t;
               merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
               sort ihet t;
               save ${gdtadir}/dta_MSM_Profiles.dta, replace;
           restore;
       };
       foreach jjv in A {;
           preserve;
               sort `lvhet' t;
               statsby corr_H_`jjv'=r(rho), by(`lvhet' t) clear: corr H `jjv';
               `lvhetcmd';
               sort ihet t;
               merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
               sort ihet t;
               save ${gdtadir}/dta_MSM_Profiles.dta, replace;
           restore;
       };
       *lnw_correlation;
       preserve;
           sort `lvhet' t;
           statsby corr_lnw=r(rho), by(`lvhet' t) clear: corr lnw lnw_next;
           `lvhetcmd';
           sort ihet t;
           merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
           sort ihet t;
           save ${gdtadir}/dta_MSM_Profiles.dta, replace;
       restore;
   };  // if (1>10 &

   *** retirement age', which might be defined as the last period someone works;
   preserve;
       keep if lfpr == 1;
       sort id t;
       by id: keep if [_n]==[_N];
       sort `lvhet' t;
       collapse (count) retage = lfpr, by(`lvhet' t);
       `lvhetcmd';
       sort ihet t;
       by ihet: egen tot = total(retage);
       replace retage = retage / tot;
       keep ihet t retage;
       sort ihet t;
       merge 1:1 ihet t using ${gdtadir}/dta_MSM_Profiles.dta, nogenerate;
       sort ihet t;
       save ${gdtadir}/dta_MSM_Profiles.dta, replace;
   restore;
};  // ivhet


*label variables;
use ${gdtadir}/dta_MSM_Profiles.dta, clear;
label_profiles; 
sort ihet t;
save ${gdtadir}/dta_MSM_Profiles.dta, replace;


